<?php
namespace App\Goods\Http\Controllers;

use App\ApiBaseController;
use App\Goods\Services\GoodsAttrService;
use Illuminate\Http\JsonResponse;
use App\Handles\CrmRpcHandle;
class GoodsAttrController extends ApiBaseController
{
    use CrmRpcHandle;
    /**
     * 商品属性列表 rpc方式获取数据
     * @return JsonResponse
     */
    public function rpcAttrList()
    {
        $params = request()->input();
        $goodsAttrService = new GoodsAttrService();
        try{
            //$res = $goodsAttrService->attrList($params);
            // 请求CRM RPC接口登录
            $res = $this->getrpcAttrList($params);
            
            return $this->success('成功',$res);
        }catch (\Exception $exception){
            return $this->error($exception->getMessage());
        }
    }
    /**
     * 商品属性列表
     * @return JsonResponse
     */
    public function attrList()
    {
        $params = request()->input();
        $goodsAttrService = new GoodsAttrService();
        try{
            $res = $goodsAttrService->attrList($params);
            return $this->success('成功',$res);
        }catch (\Exception $exception){
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 属性添加
     * @return JsonResponse
     */
    public function attrAdd()
    {
        $params = request()->input();
        if(empty($params['name'])){
            return $this->error('属性名称不能为空！');
        }
        if(empty($params['type'])){
            return $this->error('请选择属性类型！');
        }
        $goodsAttrService = new GoodsAttrService();
        try{
            $res =$goodsAttrService->attrAdd($params);
            return $this->success('添加成功!',$res);
        }catch (\Exception $exception){
            return $this->error($exception->getMessage());
        }
    }

    //属性编辑
    public function attrEdit()
    {
        $params = request()->input();
        if(empty($params['id'])){
            return $this->success('属性id不能为空！');
        }
        if(empty($params['name'])){
            return $this->success('属性名称不能为空！');
        }
        $goodsAttrService =new GoodsAttrService();
        try{
            $res =$goodsAttrService->attrEdit($params);
            return $this->success('修改成功!',$res);
        }catch (\Exception $exception){
            return $this->error($exception->getMessage());
        }
    }
}
